package com.wcy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcy.entity.LearningResource;
import com.wcy.entity.ResourceTagMapping;
import com.wcy.mapper.ResourceTagMappingMapper;
import com.wcy.service.IResourceTagMappingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴传亿
 * @since 2025-04-27
 */
@Service
public class ResourceTagMappingServiceImpl extends ServiceImpl<ResourceTagMappingMapper, ResourceTagMapping> implements IResourceTagMappingService {
    @Autowired
    private ResourceTagMappingMapper resourceTagMappingMapper;

    @Transactional
    @Override
    public Boolean replaceResourceTagMapping(LearningResource resource) {
        if(resource.getResourceId()!=null&&resource.getTags()!=null&&!resource.getTags().isEmpty()){
            resourceTagMappingMapper.delete(new QueryWrapper<ResourceTagMapping>().eq("resource_id",resource.getResourceId()));
            resourceTagMappingMapper.insert(resource
                    .getTags()
                    .stream()
                    .map(e->new ResourceTagMapping(resource.getResourceId(),e.getTagId()))
                    .collect(Collectors.toList()));
            return true;
        }
        return false;
    }
}
